package com.example.netty.netty.TCP粘包拆包问题.解决方案;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;

import java.util.UUID;

public class ServerHandler extends SimpleChannelInboundHandler<MessageProtocol> {
    private int count = 0;

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, MessageProtocol msg) throws Exception {

        String message = new String(msg.getContent(), CharsetUtil.UTF_8);
        System.out.println(message+" "+msg.getLen());
        System.out.println("接收到的长度："+(++count));
        //回复消息
        String s = UUID.randomUUID().toString();
        ctx.writeAndFlush(new MessageProtocol(s.length(),s.getBytes(CharsetUtil.UTF_8)));
    }
}
